import axios from 'axios';
import Vue from 'vue';
import { Message } from 'element-ui';
import NProgress from 'nprogress';
import router from '@/router/index';

axios.defaults.baseURL = process.env.BASE_URL;
axios.interceptors.request.use(
  (config) => {
    NProgress.start();
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.token = token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

axios.interceptors.response.use(
  // (response) => response,
  (response) => {
    console.log('正在从返回头中获取token');
    const headers = response.headers;
    console.log(headers);
    const authorizationFromHeaders = headers['authorization'];
    console.log(authorizationFromHeaders);
    const tokenFromHeaders = authorizationFromHeaders
      ? authorizationFromHeaders.split(' ')[1]
      : null;

    console.log(tokenFromHeaders);

    if (tokenFromHeaders) {
      localStorage.setItem('token', tokenFromHeaders);
      console.log('token续签成功');
      console.log(localStorage.getItem('token'));
    }

    return response;
  },
  (error) => Promise.resolve(error.response)
);

function checkStatus(response) {
  console.log('走http检查');
  NProgress.done();
  if (response.status === 200 || response.status === 304) {
    return response;
  }
  if (response.status === 401) {
    console.log('发现未授权，准备跳转到login');
    router.replace('/login');
    return response;
  }
  if (response.status >= 500) {
    return {
      data: {
        code: -500,
        status: false,
        message: response.statusText,
        data: response.statusText,
      },
    };
  }

  return {
    data: {
      code: -404,
      status: false,
      message: response.statusText,
      data: response.statusText,
    },
  };
}

function checkCode(res, errMsg) {
  console.log(res);
  if (!res.data.status) {
    switch (res.data.code) {
      case -404:
        Message.error(res.data.message || '未知异常');
        break;
      case -500:
        Message.error('服务器连接错误！');
        break;
      case 70005:
        Message.error('您需要重新登录！');
        break;
      default:
        errMsg
          ? Message.error(errMsg)
          : Message.error(res.data.message || '身份认证过期，请重新登录');
    }
  }
  return res.data;
}

export default {
  POST(url, data, errMsg) {
    const CancelToken = axios.CancelToken;
    return axios
      .post(url, data, {
        timeout: 30000,
        cancelToken: new CancelToken(function executor(c) {
          Vue.$httpRequestList.push(c);
        }),
      })
      .then(checkStatus)
      .then((res) => checkCode(res, errMsg));
  },
  GET(url, params, errMsg) {
    const CancelToken = axios.CancelToken;
    return axios
      .get(url, {
        params: {
          _t: +new Date(),
          ...params,
        },
        timeout: 30000,
        cancelToken: new CancelToken(function executor(c) {
          Vue.$httpRequestList.push(c);
        }),
      })
      .then(checkStatus)
      .then((res) => checkCode(res, errMsg));
  },
  DELETE(url, errMsg) {
    const CancelToken = axios.CancelToken;
    return axios
      .delete(url, {
        timeout: 30000,
        cancelToken: new CancelToken(function executor(c) {
          Vue.$httpRequestList.push(c);
        }),
      })
      .then(checkStatus)
      .then((res) => checkCode(res, errMsg));
  },
  PUT(url, data, errMsg) {
    const CancelToken = axios.CancelToken;
    return axios
      .put(url, data, {
        timeout: 30000,
        cancelToken: new CancelToken(function executor(c) {
          Vue.$httpRequestList.push(c);
        }),
      })
      .then(checkStatus)
      .then((res) => checkCode(res, errMsg));
  },
  EXPORT(url, data, errMsg) {
    const CancelToken = axios.CancelToken;
    return axios
      .post(url, data, {
        responseType: 'blob',
        timeout: 30000,
        cancelToken: new CancelToken(function executor(c) {
          Vue.$httpRequestList.push(c);
        }),
      })
      .then(checkStatus)
      .then((res) => res.data);
  },
};
